/**
 * 
 */
package com.game.astd.spirit;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Iterator;
import java.util.List;

import org.apache.http.Header;
import org.apache.http.message.BasicNameValuePair;

import com.game.astd.spirit.inter.AbstractXmlActionAfterRequest;
import com.game.astd.spirit.inter.TaskUnit;
import com.game.astd.spirit.model.UserData;
import com.game.astd.spirit.model.XmlDataUtil;
import com.game.astd.spirit.model.XmlNodeData;

/**
 * 宴会任务
 * 
 * @author beedoorwei
 * 
 */
public class DinnerTask extends TaskUnit
{

	public DinnerTask()
	{
		this.taskDesc = "宴会任务";
	}

	@Override
	public void doTask()
	{
		try
		{
			if (!GameUserInfoUtil.getIsUserLoadSuc())
			{
				TaskManager.addTask(this, GameUtil.randomInt(10));
			} else
			{
				Iterator<Integer> ite = GameUserInfoUtil.userIte();
				while (ite.hasNext())
				{
					int gid = ite.next();
					UserData u = GameUserInfoUtil.getUser(gid);
					if (u.getConfigValue("openDinner", "0").equals("1"))
					{
						TaskManager.addTask(new Dinner(gid));
					}
				}
			}
		} catch (Exception e)
		{
			LoggerUtil.error(0, e);
		}
	}

	/**
	 * 宴会任务
	 * 
	 * @author Administrator
	 * 
	 */
	class Dinner extends TaskUnit
	{
		public Dinner(Integer gid)
		{
			this.gid = gid;
		}

		public void doTask()
		{
			try
			{
				if (isInDinnerTime())
				{
					UserData u = GameUserInfoUtil.getUser(gid);
					if (u.getUserState() == UserState.LOGIN_GAME)
					{
						HttpUtil.doGet(gid, GameConstants.GET_DINNER_URL, new PreDinnerAction(gid));
					} else
					{
						// 添加登录任务先
						TaskManager.addTask(new LoginTask(gid, this));
					}
				}
			} catch (Exception e)
			{
				LoggerUtil.error(gid, e);
			}
		}
	}

	/**
	 * 是否是宴会时间
	 * 
	 * @return
	 */
	private boolean isInDinnerTime()
	{
		int hour = Calendar.getInstance().get(Calendar.HOUR_OF_DAY);
		if (hour >= 10 && hour < 14)
		{
			return true;
		}
		if (hour >= 19 && hour < 21)
		{
			return true;
		}
		return false;
	}

	/**
	 * 宴会前查询
	 * 
	 * @author Administrator
	 * 
	 */
	class PreDinnerAction extends AbstractXmlActionAfterRequest
	{
		public PreDinnerAction(Integer gid)
		{
			super();
			this.gid = gid;
			this.taskUnit = new Dinner(gid);
		}

		@Override
		public void doCall(List<Header> headerList, XmlNodeData dinnerInfo)
		{
			UserData u = GameUserInfoUtil.getUser(gid);

			// 判断是否还有宴会次数
			if (isHaveDinnerTimes(dinnerInfo) && isInDinnerTime())
			{
				doDinner(u, dinnerInfo);
			}
		}

		/**
		 * 开始宴会
		 */
		private void doDinner(UserData u, XmlNodeData dinnerInfo)
		{
			String usernation = GameUtil.getNation(u.getGameInfo("nation"));
			List<XmlNodeData> tempNode = XmlDataUtil.getNodeInfoList(dinnerInfo, "results.team.nation");

			String teamstate = XmlDataUtil.getNodeData(dinnerInfo, "results.teamstate");
			if (teamstate.equals("1"))
			{
				List<XmlNodeData> teampInfo = XmlDataUtil.getNodeInfoList(dinnerInfo, "results.creator.name");
				XmlNodeData isLeaderNode = null;
				for (XmlNodeData x : teampInfo)
				{
					String creator = x.getNodeValue();
					if (creator.equals(u.getGameInfo("game_nick_name")))
					{
						isLeaderNode = x;
						break;
					}
				}
				if (isLeaderNode != null)
				{
					String num = XmlDataUtil.getNodeData(isLeaderNode, "num");
					if (GameUtil.convertInt(num, 0) >= GameUtil.convertInt(u.getConfigValue("dinnerMinNum", "0")))
					{
						List<BasicNameValuePair> nvpList = new ArrayList<BasicNameValuePair>();
						HttpUtil.doPost(gid, GameConstants.START_DINNER_URL, nvpList, new DinnerTeamAction(gid, DinnerType.JOIN));
					} else
					{
						TaskManager.addTask(new Dinner(gid), GameUtil.randomInt(10));
					}
				} else
				{
					TaskManager.addTask(new Dinner(gid), GameUtil.randomInt(10) + 5);
				}
			} else
			{
				List<XmlNodeData> nationTeamList = new ArrayList<XmlNodeData>();
				// 自身创建组队信息，如果不为空，则是自己创建了组队，需要开启宴会，而非加入或者创建组队
				if (!tempNode.isEmpty())
				{
					// 不为空，有组队，查查是否有本国籍的队伍
					for (XmlNodeData cn : tempNode)
					{
						if (cn.getNodeValue().indexOf(usernation) >= 0)
						{
							// 判断是否已经满了
							String num = XmlDataUtil.getNodeData(cn.getParent(), "num");
							if (GameUtil.convertInt(num) < 7)
							{
								nationTeamList.add(cn.getParent());
							}
						}
					}
				}

				if (nationTeamList.isEmpty())
				{
					// 创建组队，并且在5秒后查看队伍是否已经满了
					List<BasicNameValuePair> nvpList = new ArrayList<BasicNameValuePair>();
					nvpList.add(new BasicNameValuePair("dinnerType", "0"));
					HttpUtil.doPost(gid, GameConstants.DO_DINNER_TEAM_URL, nvpList, new DinnerTeamAction(gid, DinnerType.CREATE));
				} else
				{
					String teamid = XmlDataUtil.getNodeData(nationTeamList.get(0), "teamid");
					// 加入组队，并且在10秒后查看，判断是否有
					List<BasicNameValuePair> nvpList = new ArrayList<BasicNameValuePair>();
					nvpList.add(new BasicNameValuePair("teamId", teamid));
					HttpUtil.doPost(gid, GameConstants.JOIN_DINNER_URL, nvpList, new DinnerTeamAction(gid, DinnerType.JOIN));
				}
			}
		}

		/**
		 * 判断是否还有宴会次数
		 * 
		 * @param dinnerInfo
		 * @return
		 */
		private boolean isHaveDinnerTimes(XmlNodeData dinnerInfo)
		{
			// 在组队中，则认为可以
			String teamstate = XmlDataUtil.getNodeData(dinnerInfo, "results.teamstate");
			if (teamstate.equals("1"))
			{
				return true;
			}

			String times = XmlDataUtil.getNodeData(dinnerInfo, "results.normaldinner.num");
			if (GameUtil.convertInt(times) > 0)
			{
				return true;
			}
			return false;
		}
	}

	class DinnerTeamAction extends AbstractXmlActionAfterRequest
	{
		DinnerType type;

		public DinnerTeamAction(Integer gid, DinnerType type)
		{
			super();
			this.gid = gid;
			this.type = type;
			this.taskUnit = new Dinner(gid);
		}

		@Override
		public void doCall(List<Header> headerList, XmlNodeData dinnerInfo)
		{
			LoggerUtil.info(gid, type.desc + "组队成功");
			TaskManager.addTask(new Dinner(gid), GameUtil.randomInt(10));
		}
	}

}

enum DinnerType {
	JOIN(1, "加入组队"), CREATE(2, "创建组队");
	public int value;
	public String desc;

	private DinnerType(int v, String desc)
	{
		this.value = v;
		this.desc = desc;
	}
}
